/*************************************
 * Created by WebStorm
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time :2025/1/21 15:29
 Desc :
 **************************************/
import {defineStore} from "pinia";
import Logo from '@assets/images/logo1.svg'
import {Config} from '@/config.ts'

import routes from "@/router/routes.ts";
import {UseUserStore} from "@/store/models/user.ts";
import {store} from "@/store";

type AppsState = {
    serverTime: Date, //服务器时间
    localTime: Date, //本机时间
    title: string | any,//系统标题(名称)
    logo?: string | number | null | any | bigint,//logo
    copyright: string,
    icp?: string,
    icp_url: string,
    tel_email_qq: string,
    routers: Array<any>,
    menu: Array<any>
}

export const useAppStore = defineStore('app', {
    state: (): AppsState => ({
        serverTime: new Date(),
        localTime: new Date(),
        title: Config.Title,// 系统标题(名称)
        logo: Logo, // logo
        copyright: '',
        icp: '',
        icp_url: '',
        tel_email_qq: '',
        routers: [],
        menu: []
    }),
    getters: {
        renderMenu(state) {
            const menu = state.routers[1].children ?? [];
            state.menu = menu
                .filter((item:any) => !item.meta.hideInMenu) // 过滤掉隐藏的菜单项和不需要的菜单项
                .map((item:any)  => ({
                    title: item.meta.title,
                    key: item.name,
                    icon: item.meta.icon
                }))
        }
    },
    actions: {
        updateLocalTime() {
            this.localTime = new Date(this.localTime.getTime() + 1000);
        },
        //预处理路由
        preProcessingRoutes() {
            const userStore = UseUserStore()
            const filteredRoutes = routes.map(route => {
                if (route.children) {
                    route.children = route.children.filter(child => userStore.regional || child.name !== 'salesmans'); // 过滤子菜单
                }
                return route;
            })
            this.routers = filteredRoutes;
            return filteredRoutes;
        }
    }
});

export function useAsyncRouteStoreWidthOut() {
    return useAppStore(store);
}